﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace BitcoinchartsViewer.Classes
{
    public static class Utils
    {
        static public DateTime conv_Timestamp2Date(int Timestamp)
        {
            //  gerechnet wird ab der UNIX Epoche
            System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
            // den Timestamp addieren           
            dateTime = dateTime.AddSeconds(Timestamp);
            return dateTime;
        }
        static public Int32 conv_Date2Timestamp(DateTime Date)
        {
            //  gerechnet wird ab der UNIX Epoche
            System.DateTime epoche = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
            System.DateTime dateTime = Date;
            TimeSpan ts = new TimeSpan(dateTime.Ticks - epoche.Ticks);  // das Delta ermitteln
            // Das Delta als gesammtzahl der sekunden ist der Timestamp
            return (Convert.ToInt32(ts.TotalSeconds));
        }
        public static string GetCSV(String datasource, DateTime Von, DateTime Bis)
        {
            String content;
            Int32 von = Classes.Utils.conv_Date2Timestamp(Von.AddHours(-2));
            Int32 bis = Classes.Utils.conv_Date2Timestamp(Bis.AddHours(-2));
            using (WebClient wc = new WebClient())
                content = wc.DownloadString(String.Format("http://bitcoincharts.com/t/trades.csv?symbol={0}&start={1}&end={2}", datasource, von, bis));
            return content;
        }
        public static System.Data.DataTable GetDataTable(string trades, char seperator, System.Data.DataTable datatable)
        {
            DateTime date = new DateTime();
            Double price;
            Double amount;
            if (datatable.Columns.Count == 0)
            {
                datatable.Columns.Add("Datum", typeof(DateTime));
                datatable.Columns.Add("Preis", typeof(Double));
                datatable.Columns.Add("Menge", typeof(Double));
            }
            if (!String.IsNullOrEmpty(trades))
            {

                using (StringReader sr = new StringReader(trades))
                {
                    String strLine = String.Empty;
                    String[] strArray = new String[3];

                    //DataRow dr = dt.NewRow();
                    int Counter = 0;
                    while (sr.Peek() > -1)
                    {
                        strLine = sr.ReadLine();
                        strArray = strLine.Split(seperator);
                        date = Classes.Utils.conv_Timestamp2Date(Convert.ToInt32(strArray[0]));
                        price = Double.Parse(strArray[1], System.Globalization.CultureInfo.InvariantCulture);
                        amount = Double.Parse(strArray[2], System.Globalization.CultureInfo.InvariantCulture);
                        if (datatable.Rows.Count > Counter)
                        {
                            datatable.Rows.RemoveAt(0);
                        }
                        datatable.Rows.Add(date.AddHours(2), price, amount);
                        Counter++;
                    }
                }
            }
            return datatable;
        }
    }
}
